package me.hys.action.resource;

import java.io.File;
import java.util.Map;

import me.hys.api.BaseApi;
import me.hys.base.AjaxResult;
import me.hys.base.BaseAction;
import me.hys.entity.resource.RadioEntity;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 * Author: @author HYS . <br>
 * Date: 2015-12-23 下午8:29:21 . <br>
 * Function: 视频
 */
@Controller
@RequestMapping("/radio")
public class RadioAction extends BaseAction {
	public RadioAction() {
		setFlag(getClass().getSimpleName());
	}

	/**
	 * @Title : add
	 * @Description : 添加页面
	 */
	@RequestMapping("/add")
	public ModelAndView add() {
		Map<String, Object> data = getMap();
		data.put("entity", new RadioEntity());
		return new ModelAndView("/resource/radio_form", data);
	}

	/**
	 * @Title : edit
	 * @Description : 修改页面
	 */
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable String id) {
		Map<String, Object> data = getMap();
		data.put("entity", new RadioEntity());
		return new ModelAndView("/resource/radio_form", data);
	}

	/**
	 * @Title : index
	 * @Description : 列表
	 */
	@RequestMapping("/index")
	public ModelAndView index() {
		Map<String, Object> data = getMap();
		// PageQuery<CaseBo> pageQuery = getPageQuery();
		// data.put("data", caseManager.findPageBo(pageQuery));
		return new ModelAndView("/resource/radio", data);
	}

	/**
	 * @Title : save
	 * @Description : 保存
	 */
	@RequestMapping("/save")
	@ResponseBody
	public AjaxResult save(@ModelAttribute RadioEntity entity,
			@RequestParam(required = false) MultipartFile upfile,
			@RequestHeader(value = "referer") String url) {
		int userid = getUser().getId();
		if (upfile.getSize() > 0) {
			String path = request.getSession().getServletContext()
					.getRealPath("/" + BaseApi.UPLOAD_PATH);
			String userDir = path + "/" + BaseApi.USER_UPLOAD_PATH_PREFIX
					+ userid;
			// 创建用户目录
			File userDirFile = new File(userDir);
			if (!userDirFile.exists()) {
				userDirFile.mkdir();
			}
			// 原文件名
			String originalFilename = upfile.getOriginalFilename();
			String[] fileParts = originalFilename.split("\\.");
			String fileNewName = !StringUtils.isEmpty(entity.getName()) ? entity
					.getName() : System.currentTimeMillis() + "";
			String suffix = "." + fileParts[fileParts.length - 1];
			// 上传的图片
			File file = new File(userDir, fileNewName + suffix);
			try {
				upfile.transferTo(file);
				String fileSavePath = BaseApi.UPLOAD_PATH + "/"
						+ BaseApi.USER_UPLOAD_PATH_PREFIX + userid + "/"
						+ fileNewName + suffix;
				entity.setName(fileNewName);
				entity.setOriginalName(originalFilename);
				entity.setUrl(fileSavePath);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		AjaxResult ajaxResult = new AjaxResult();
		ajaxResult.setMsg(url);
		return ajaxResult;
	}
}
